package com.gagakuai.bpm.service.camunda.impl;



import com.gagakuai.bpm.dao.mapper.camunda.InformMapper;
import com.gagakuai.bpm.domian.dto.camunda.HiComment;
import com.gagakuai.bpm.domian.dto.camunda.HiTaskInst;
import com.gagakuai.bpm.service.camunda.InformService;
import com.gagakuai.bpm.utils.DateUtils;
import com.gagakuai.bpm.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class InformImpl implements InformService {

    @Resource
    private InformMapper informMapper;

    @Resource
    private HistoryService historyService;

    @Resource
    private TaskService taskService;

    @Resource
    private InformImpl inform;

    @Override
    public int insertTaskInst(List<HiTaskInst> list) {
        return informMapper.insertTaskInst(list);
    }

    @Override
    public String readed(String taskId, String reason) {
        HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
        if(ObjectUtils.isEmpty(historicTaskInstance)){
            return "当前任务ID未查询到任务";
        }

        String procInstId = historicTaskInstance.getProcessInstanceId();
        if(StringUtils.isBlank(procInstId)){
            return "流程实例ID不能为空";
        }

        HistoricActivityInstance historicActivityInstance = historyService.createHistoricActivityInstanceQuery()
                                                            .activityInstanceId(historicTaskInstance.getActivityInstanceId())
                                                            .singleResult();

        if(ObjectUtils.isEmpty(historicActivityInstance)){
            return "当前任务ID未查询到节点实例";
        }

        HiComment hiComment = informMapper.queryHiCommentByTaskIdAndProcInstId(taskId, procInstId);
        return inform.save(taskId, reason, procInstId, hiComment, historicActivityInstance, historicTaskInstance);
    }

    @Transactional(rollbackFor = Exception.class)
    public String save(String taskId, String reason, String procInstId, HiComment hiComment,
                       HistoricActivityInstance historicActivityInstance,
                       HistoricTaskInstance historicTaskInstance){
        if(ObjectUtils.isEmpty(hiComment)){
            taskService.createComment(taskId, procInstId, reason);
        }else{
            informMapper.updateHiCommentById(hiComment.getId(), reason);
        }


        Date  date = new Date();
        informMapper.updateHiTask(taskId, (int) (date.getTime() - historicTaskInstance.getStartTime().getTime()),
                DateUtils.formatDatetime(date),
                "completed");

        informMapper.updateHiActInst(historicActivityInstance.getId(), 4,
                (int) (date.getTime() - historicActivityInstance.getStartTime().getTime()),
                DateUtils.formatDatetime(date));

        return null;
    }
}
